package com.yc.pre.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;

/**
 * @ClassName MathUtils
 * @Description 计算百分比
 * @Date 2021/02/08 16:36:08
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
public class MathUtils {

    public static String caculateRate(Integer num1, Integer num2){
        if(ObjectUtils.isEmpty(num1) || ObjectUtils.isEmpty(num2)){
            return "0.00%";
        }
        if(num1 == 0 || num2 == 0){
            return "0.00%";
        }
        // 格式化为两位小数
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        float result = (float) num1 * 100 / num2;
        String format = decimalFormat.format(result);
        return format + "%";
    }

    public static void main(String[] args) {
        System.out.println(caculateRate(12, 55));
    }

}
